package com.bdqn.service;

import com.bdqn.common.PageSupport;
import com.bdqn.mapper.SysUserMapper;
import com.bdqn.pojo.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * SysUserServiceImpl
 *
 * @author LILIBO
 * @since 2022-12-22
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 添加用户
     *
     * @param sysUser
     * @return
     */
    @Override
    public boolean add(SysUser sysUser) {
        Date now = new Date();
        sysUser.setCreatedTime(now); // 设置创建时间
        sysUser.setUpdatedTime(now); // 设置修改时间
        return sysUserMapper.add(sysUser) == 1;
    }

    /**
     * 获取所有系统用户
     *
     * @return
     */
    @Override
    public List<SysUser> getList() {
        return sysUserMapper.getList();
    }

    /**
     * 用户登录
     *
     * @param account 账号
     * @param password 密码
     * @return 当前登录对象
     */
    @Override
    public SysUser login(String account, String password) {
        if (account == null || password == null) {
            return null;
        }
        return sysUserMapper.getUser(account, password);
    }

    /**
     * 获取数据总数
     *
     * @param queryRealName
     * @param queryRoleId
     * @return
     */
    @Override
    public int getCount(String queryRealName, Long queryRoleId) {
        return sysUserMapper.getCount(queryRealName, queryRoleId);
    }

    /**
     * 分页查询用户列表数据
     *
     * @param queryRealName
     * @param queryRoleId
     * @param page
     * @return
     */
    @Override
    public List<SysUser> getPage(String queryRealName, Long queryRoleId, PageSupport page) {
        // limit 0, 10
        int pageSize = page.getPageSize();
        int pageIndex = (page.getCurrentPageNo() - 1) * pageSize;
        return sysUserMapper.getPage(queryRealName, queryRoleId, pageIndex, pageSize);
    }

    /**
     * 通过ID获取用户
     *
     * @param id
     * @return
     */
    @Override
    public SysUser getById(Integer id) {
        return sysUserMapper.getById(id);
    }

    /**
     * 通过账号获取用户
     *
     * @param account
     * @return
     */
    @Override
    public SysUser getByAccount(String account) {
        return sysUserMapper.getByAccount(account);
    }

}
